﻿//
// GNS_PvEAllDatePlay.cpp
// MobileManage
//
// Created by MR.L on 16/2/18.
//
//
#include "GnsClientFrame.h"
#include "GNS_PvEAllDatePlay.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

//extern ISocketMgr * _GameSock;

GNS_PvEAllDatePlay::GNS_PvEAllDatePlay()
{
    pGNS_PvEAllDatePlay=0;
    DataListView=nullptr;
    pPlayName=nullptr;
    TimeText=nullptr;
    pCalendar=nullptr;
    TitleText=nullptr;
    GameType=nullptr;
    GameName=nullptr;
    UplineID=0;
    CurID=0;
    Year=0;
    Mon=0;
    Day=0;
}

GNS_PvEAllDatePlay::~GNS_PvEAllDatePlay()
{
    
}

GNS_PvEAllDatePlay* GNS_PvEAllDatePlay::createLayer()
{
    auto pGNS_PvEAllDatePlay = GNS_PvEAllDatePlay::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_PvEAllDatePlay);
    
    return pGNS_PvEAllDatePlay;
}

bool GNS_PvEAllDatePlay::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    /**
     *  @author MR.L, 16-02-16 11:02:50
     *
     *  获取当前时间
     */
    time_t tt;
    time(&tt);
    struct tm * time;
    time = localtime(&tt);
    
    Mon = (int)time->tm_mon+1;
    Year = time->tm_year+1900;
    Day  =(int)time->tm_mday;
    return true;
}
void GNS_PvEAllDatePlay::SetNodeName(std::string CsbName)
{
    pGNS_PvEAllDatePlay = CSLoader::createNode(CsbName);
    pGNS_PvEAllDatePlay->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_PvEAllDatePlay);
    addChild(pGNS_PvEAllDatePlay,11);
    
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_PvEAllDatePlay);
    
    auto pBackBtn = static_cast<Button*>(pGNS_PvEAllDatePlay->getChildByName("PvPApplyTickBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDatePlay::BtnCallBack, this));

	auto title = static_cast<Text*>(pGNS_PvEAllDatePlay->getChildByName("Text_5"));//Text_5
    
    DataListView =static_cast<ListView*>(pGNS_PvEAllDatePlay->getChildByName("DateListView"));
    
    pPlayName = static_cast<Text*>(pGNS_PvEAllDatePlay->getChildByName("Text_4"));
    TimeText=static_cast<Text*>(pGNS_PvEAllDatePlay->getChildByName("TimeText"));

	if (GNS_PlayerMgr::getInstance()->getPlayProPlayFit()->size() > 0)
	{
		GNS_PlayerMgr::getInstance()->transferEachTime(Year, Mon, Day, GNS_PlayerMgr::getInstance()->getPlayProPlayFit()->at(0)._selDate);
	}
	TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,Day));
    
    GameName= static_cast<Text*>(pGNS_PvEAllDatePlay->getChildByName("Text_3_0"));
	if (GNS_PlayerMgr::getInstance()->getGameId() > 0)
	{
		GameName->setString(GNS_PlayerMgr::getInstance()->getGameIdInfo()[GNS_PlayerMgr::getInstance()->getGameId()]);
	}
    
    GameType = static_cast<ListView*>(pGNS_PvEAllDatePlay->getChildByName("GameType"));
    GameType->removeAllItems();
    
	int x = 0;
    for (auto it:(* GNS_PlayerMgr::getInstance()->getGameList())) {
        if (it.iGameType==1) {
            auto pbutton = Button::create("p0Btn.png");
			pbutton->setTitleText(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
            pbutton->setTitleColor(Color3B::BLACK);
            pbutton->setTitleFontSize(20);
            pbutton->setTag(7);
            pbutton->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDatePlay::BtnCallBack, this));
            GameType->addChild(pbutton);

			++x;
			if (x == 1)
				GameName->setString(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
        }

    }
    GameType->setVisible(false);

    auto pTimeBtn = static_cast<Button*>(pGNS_PvEAllDatePlay->getChildByName("TimeBtn"));
    pTimeBtn->setTag(2);
	pTimeBtn->setVisible(false);
    //pTimeBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDatePlay::BtnCallBack, this));
    
    auto pTimeSer = static_cast<Button*>(pGNS_PvEAllDatePlay->getChildByName("Button_8"));
    pTimeSer->setTag(2);
    //pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDatePlay::BtnCallBack, this));
    
    auto typrbtnSrt = static_cast<Button*>(pGNS_PvEAllDatePlay->getChildByName("Button_8_0"));
    typrbtnSrt->setTag(3);
    //typrbtnSrt->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDatePlay::BtnCallBack, this));
    
    auto gamebtn1 = static_cast<Button*>(pGNS_PvEAllDatePlay->getChildByName("GameBtn"));
    gamebtn1->setTag(3);
	gamebtn1->setVisible(false);
    //gamebtn1->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDatePlay::BtnCallBack, this));
    
    TitleText = static_cast<Text*>(pGNS_PvEAllDatePlay->getChildByName("Text_5"));

	auto pve = static_cast<ImageView*>(pGNS_PvEAllDatePlay->getChildByName("PlayerPve"));
	auto pvp = static_cast<ImageView*>(pGNS_PvEAllDatePlay->getChildByName("PlayerPvp"));
	pvp->setVisible(false);
	pve->setVisible(true);

	AcceptSeverDate();
}
void GNS_PvEAllDatePlay::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
			/*	if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size() >= 3) {
					GNS_PlayerMgr::getInstance()->_NodeChildVec.erase((int)GNS_PlayerMgr::getInstance()->_NodeChildVec.size() - 1);
					this->removeAllChildrenWithCleanup(true);
					this->getParent()->removeChild(this);
					auto PromoterNode = GNS_PlayerMgr::getInstance()->_NodeChildVec.at((int)GNS_PlayerMgr::getInstance()->_NodeChildVec.size() - 2);
					PromoterNode->setVisible(true);
				}
				*/
				if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size() > 0)
				{
					GNS_PlayerMgr::getInstance()->_NodeChildVec.erase((int)GNS_PlayerMgr::getInstance()->_NodeChildVec.size() - 1);
					this->removeAllChildrenWithCleanup(true);
					this->setVisible(false);
					//this->getParent()->removeChild(this);
				}
				
				if (GNS_PlayerMgr::getInstance()->getUserLevel() == 0)
				{
					//if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size() != 0) {
					//	GNS_PlayerMgr::getInstance()->_NodeChildVec.erase(1);
					//	this->removeAllChildrenWithCleanup(true);
					//	this->setVisible(false);
					//	//this->getParent()->removeChild(this);
					//}
					//auto PromoterNode = GNS_PlayerMgr::getInstance()->_NodeChildVec.at(0);
					//PromoterNode->setVisible(true);
					CMD_CS_GetManToMachPlayProfit pGetPlayPro;
					pGetPlayPro._uplineCode = UplineID;
					pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
					pGetPlayPro._gameId = GNS_PlayerMgr::getInstance()->getGameId();
					pGetPlayPro._type = 2;
					std::string TimeDate = StringUtils::format("%d-%02d-%02d", Year, Mon, Day);
					memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
					GnsClientFrame::getInstance()->sendMsg(0, MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
					GNS_PlayerMgr::getInstance()->getPlayProProFit()->clear();
				}

				

				auto PromoterNode = GNS_PlayerMgr::getInstance()->_NodeChildVec.at((int)GNS_PlayerMgr::getInstance()->_NodeChildVec.size() -1);
				if (PromoterNode)
					PromoterNode->setVisible(true);
                break;
            }
            case 2:
            {
                /*pCalendar =GNS_Calendar::createCalendar();
                pCalendar->SetCalendBgImage("Calendarbg.png");
                std::vector<Button *> CalenderBtn;
                pCalendar->GetTimeButton()->swap(CalenderBtn);
                for (auto & it:CalenderBtn) {
                    it->setTag(100);
                    it->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDatePlay::TimeButtonCallBack, this));
                }*/
				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_PvEAllDatePlay::TimeButtonCallBack, this));
                break;
            }
            case 3:
            {
                if (GameType->isVisible()==true) {
                    GameType->setVisible(false);
                }else{
                    GameType->setVisible(true);
                }
                break;
            }
            case 7:
            {
                GameName->setString(pButton->getTitleText());
				auto st = SystemMgr::getInstance()->utf8ToGb(pButton->getTitleText().c_str());
				std::map<string, int>::iterator mapGameNameIter = GNS_PlayerMgr::getInstance()->getGameNameInfo().find(st);
				if (mapGameNameIter != GNS_PlayerMgr::getInstance()->getGameNameInfo().end())
				{
					GNS_PlayerMgr::getInstance()->getPlayProPlayFit()->clear();
					CMD_CS_GetManToMachPlayProfit pGetPlayPro;
					pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
					pGetPlayPro._uplineCode = CurID;
					std::string TimeStr = StringUtils::format("%d-%02d-%02d", Year, Mon, Day);
					memcpy(pGetPlayPro._selDate, TimeStr.c_str(), 11);
					pGetPlayPro._type = 3;
					pGetPlayPro._gameId = mapGameNameIter->second;
					GnsClientFrame::getInstance()->sendMsg(0, MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
					GameType->setVisible(false);
					GNS_PlayerMgr::getInstance()->setGameId(pGetPlayPro._gameId);
				}
                break;
            }
            default:
                break;
        }
    }
}
void GNS_PvEAllDatePlay::AcceptSeverDate()
{
    int belongType;
    Size bgSize;
    int x=0;
    DataListView->removeAllItems();
    for (auto it:(*GNS_PlayerMgr::getInstance()->getPlayProPlayFit())) {
        auto Itembg = ImageView::create("line2.png");
        DataListView->addChild(Itembg);
        bgSize = Itembg->getContentSize();
        
        auto Name = Text::create(StringUtils::format("%d",it._agentCode), GameFontName, 24);
        Name->setPosition(Vec2(bgSize.width*0.11, bgSize.height*0.34));
        Name->setColor(Color3B::BLACK);
        Itembg->addChild(Name);
        
        auto lineRevenueRatio = Text::create(StringUtils::format("%lld",it._gamePlayIn), GameFontName, 24);
        lineRevenueRatio->setPosition(Vec2(bgSize.width*0.34, bgSize.height*0.34));
        lineRevenueRatio->setColor(Color3B::BLACK);
        Itembg->addChild(lineRevenueRatio);
		
        auto agentRevenueRatio = Text::create(StringUtils::format("%lld",it._gamePlayOut), GameFontName, 24);
        agentRevenueRatio->setPosition(Vec2(bgSize.width*0.58, bgSize.height*0.34));
        agentRevenueRatio->setColor(Color3B::BLACK);
        Itembg->addChild(agentRevenueRatio);
        
        auto AgameProfit = Text::create(StringUtils::format("%lld",it._gameProfit), GameFontName, 24);
        AgameProfit->setPosition(Vec2(bgSize.width*0.81, bgSize.height*0.4));
        AgameProfit->setColor(Color3B::BLACK);
        Itembg->addChild(AgameProfit);
        
        belongType= it._belongType;
        x++;
    }
    DataListView->setInnerContainerSize(Size(bgSize.width,(bgSize.height+10)*x));

    if (belongType==21) {
        TitleText->setString("推广的玩家总玩");
    }

}
void GNS_PvEAllDatePlay::SetProName(int Name,int UplineIDs)
{
	//char temp[50];
	//sprintf(temp, "推广员账号:%d", Name);
	//pPlayName->setString(temp); //    StringUtils::format()"推广员账号:%s",Name.c_str()
	std::string strAgentCode = CCGnsOften::getUtf8_DirectAssignmentInProgram("推广员账号:");
	strAgentCode.append(SystemMgr::getInstance()->GbToUTF8(StringUtils::format("%d", Name).c_str()));
	pPlayName->setString(strAgentCode);
	pPlayName->setColor(Color3B::BLACK);
	pPlayName->setVisible(true);
    CurID=Name;
    UplineID=UplineIDs;
    
}
void GNS_PvEAllDatePlay::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        std::string BtnText = pButton->getTitleText();
        Mon = pCalendar->Mon;
        Year = pCalendar->year;
        Day = atoi(BtnText.c_str());
        TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,atoi(BtnText.c_str())));
        pCalendar->Show(false);
        
        GNS_PlayerMgr::getInstance()->getPlayProPlayFit()->clear();
        CMD_CS_GetManToMachPlayProfit pGetPlayPro;
		pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetPlayPro._uplineCode=CurID;
        std::string TimeStr = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
        memcpy(pGetPlayPro._selDate, TimeStr.c_str(), 11);
        pGetPlayPro._type=3;
        pGetPlayPro._gameId=GNS_PlayerMgr::getInstance()->getGameId();
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
    }
}